<template>
  <div class="container">
    <div class="app-container">
      <div style="padding: 10px">
        <el-button
          type="primary"
          size="mini"
          @click="addPermission(0, 1)"
        >添加权限</el-button>
      </div>
      <el-table
        :data="tableData"
        default-expand-all
        row-key="id"
      >
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              size="mini"
              @click="addPermission(row.id, 2)"
            >添加</el-button>
            <el-button
              type="text"
              size="mini"
              @click="editPermission(row.id)"
            >编辑
            </el-button>
            <el-popconfirm
              title="你确定删除确定删除吗？"
              @onConfirm="delPermission(row.id)"
            >
              <el-button
                slot="reference"
                type="text"
                size="mini"
                style="margin-left: 10px"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :visible.sync="showDialog" :title="title" @close="close">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" style="width: 90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" style="width: 90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" style="width: 90%" />
        </el-form-item>
        <el-form-item label="开启" prop="state">
          <el-tooltip
            :content="'开关状态：' + (form.state === 1 ? '启用' : '未启用')"
            placement="top-start"
          >
            <el-switch
              v-model="form.state"
              :active-value="1"
              :inactive-value="0"
            />
          </el-tooltip>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button
              type="primary"
              size="small"
              @click="handleSubmit"
            >确定</el-button>
            <el-button size="small" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqGetPermissionList,
  reqDelPermission,
  reqAddPermission,
  reqGetPermissionDetail,
  reqEditPermission
} from '@/api/permission'
import { translateToTreeData } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      title: '新增权限点',
      showDialog: false, // 控制弹出框得显示与隐藏
      tableData: [],
      sourceData: [],
      formCopy: {},
      form: {
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: 0,
        enVisible: 0
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'change' },
          {
            validator: (rule, value, cb) => {
              if (value === this.formCopy.name) return cb()
              this.sourceData.find(item => value === item.name) ? cb(new Error(`${value}已经存在`)) : cb()
            }
          }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'change' },
          {
            validator: (rule, value, cb) => {
              if (value === this.formCopy.code) return cb()
              this.sourceData.find(item => value === item.code) ? cb(new Error(`${value}已经存在`)) : cb()
            }
          }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      console.log(data)
      this.sourceData = data
      console.log(this.sourceData)

      this.tableData = translateToTreeData(data, 0)
    },
    // 删除权限
    async delPermission(id) {
      await reqDelPermission(id)
      this.getPermissionList()
    },
    // 关闭弹出框
    close() {
      this.showDialog = false
      this.$refs.formRef.resetFields()
      this.form = {
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: 0,
        enVisible: 0
      }
    },
    // 打开弹出框传递数据
    addPermission(id, type) {
      this.title = '新增权限点'
      this.form.pid = id
      this.form.type = type
      this.showDialog = true
    },
    // 确定新增或者修改
    handleSubmit() {
      this.$refs.formRef.validate(async(boo) => {
        if (boo) {
          if (this.form.id) {
            await reqEditPermission(this.form)
          } else {
            await reqAddPermission(this.form)
          }
          this.showDialog = false
          this.getPermissionList()
        }
      })
    },
    // 获取数据详情
    async editPermission(id) {
      this.title = '编辑权限点'
      const res = await reqGetPermissionDetail(id)
      console.log(res.data)
      this.formCopy = { ...res.data }
      this.form = res.data
      this.showDialog = true
    }
  }
}
</script>
